package com.mida.app.report.utils;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.mida.app.report.enumData.ReportFragmentSlotInfo;
import com.mida.bms.report.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;

import java.io.*;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <h1>报告相关方法(不涉及)</h1>
 * <p>
 *
 * </p>
 *
 * @author jian.li on 2021/8/8 9:57
 */
@Slf4j
public class ReportUtil {

    /**
     * 打包初始化模板报告信息mock数据
     *
     * @param reportDoctor
     * @param info
     * @param patientInfoVo
     */
    public static void packInitializedReportInfoMock(String reportDoctor, Map<String, String> info, PatientInfoVo patientInfoVo) {
        // 书写报告师签名
        info.put(ReportFragmentSlotInfo.REPORT_DOCTOR.code.toString(), reportDoctor);
        // 患者姓名
        info.put(ReportFragmentSlotInfo.PATIENT_NAME.code.toString(), patientInfoVo.getPatientName());
        // 性别
        info.put(ReportFragmentSlotInfo.GENDER.code.toString(), patientInfoVo.getSex());
        // 年龄
        info.put(ReportFragmentSlotInfo.AGE.code.toString(), patientInfoVo.getAge());
        // 申请科室
        info.put(ReportFragmentSlotInfo.DEPARTMENT.code.toString(), patientInfoVo.getRequestDept());
        // 申请医生
        info.put(ReportFragmentSlotInfo.DOCTOR.code.toString(), "陈瑛");
        // 床号
        info.put(ReportFragmentSlotInfo.BED_NUMBER.code.toString(), patientInfoVo.getBedNo());
        // 住院号
        info.put(ReportFragmentSlotInfo.HOSPITALIZED_NUMBER.code.toString(), patientInfoVo.getHospitalNo());
        // 检查部位
        info.put(ReportFragmentSlotInfo.CHECK_POSITION.code.toString(), patientInfoVo.getExamParts());
        // 检查目的
        info.put(ReportFragmentSlotInfo.CHECK_PURPOSE.code.toString(), patientInfoVo.getExamTarget());
        // 就诊场景代码
        info.put(ReportFragmentSlotInfo.VISITING_SCENE.code.toString(), "1706");
        // 检查方法
        info.put(ReportFragmentSlotInfo.CHECK_METHOD.code.toString(), patientInfoVo.getExamItem());
        // 检查技术
        info.put(ReportFragmentSlotInfo.CHECK_TECHNICAL.code.toString(), "采用高电压和高电流，如140KV，140~210As。层厚1mm，重建间隔0.7~1mm。图像重建采用高空间分辨率算法");
        // 检查号
        info.put(ReportFragmentSlotInfo.CHECK_NO.code.toString(), "1000000");
        // 门诊号
        info.put(ReportFragmentSlotInfo.OUT_PATIENT_NO.code.toString(), "8");
        // 加急标识
        info.put("emergencySign", "20175434213752832");
    }

    /**
     * 获取一般信息,检查信息mock数据
     *
     * @param listTmp
     * @param filePatientinfo
     * @return
     */
    public static List<PatientInfoVo> gainMockPatientInfo(List<PatientInfoVo> listTmp, File filePatientinfo) {
        // 读取mock数据(一般信息,检查信息)
        FileInputStream fis = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        String json = "";
        try {
            fis = new FileInputStream(filePatientinfo);
            isr = new InputStreamReader(fis, "utf-8");
            br = new BufferedReader(isr);
            String line = null;
            while ((line = br.readLine()) != null) {
                json += line;
            }
            listTmp = JSON.parseArray(json, PatientInfoVo.class);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {

            try {
                if (br != null) {
                    br.close();
                }
                if (isr != null) {
                    isr.close();
                }

                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                log.error(e.getMessage(), e);
            }
        }
        return listTmp;
    }

    /**
     * 校验上游患者姓名是否修改
     *
     * @param request
     * @param personName
     */
    public static void checkPersonName(DocReportPreservationRequest request, String personName) {
        if (ObjectUtils.isNotEmpty(request.getDocReportVoList())) {
            DocReportVo docReportVo = request.getDocReportVoList().get(0);
            List<DocReportParagraphVo> docStructParagraphVoList = docReportVo.getDocStructParagraphVoList();
            if (CollectionUtils.isNotEmpty(docStructParagraphVoList)) {

                Map<Long, DocReportParagraphVo> docReportParagraphVoMap = docStructParagraphVoList.stream().collect(Collectors.toMap(DocReportParagraphVo::getMeaningTag, e -> e));
                DocReportParagraphVo docReportParagraphVo = docReportParagraphVoMap.get(3471L);
                if (ObjectUtils.isNotEmpty(docReportParagraphVo)) {
                    List<DocReportFragmentVo> docStructFragmentVoList = docReportParagraphVo.getDocStructFragmentVoList();
                    Map<Long, DocReportFragmentVo> docReportFragmentVoMap = docStructFragmentVoList.stream().filter(vo -> ObjectUtils.isNotEmpty(vo)).collect(Collectors.toMap(DocReportFragmentVo::getNoumenonSoid, e -> e));

                    DocReportFragmentVo docReportFragmentVo = docReportFragmentVoMap.get(3535L);
                    if (ObjectUtils.isNotEmpty(docReportFragmentVo)) {
                        List<DocStructTemplateSlotVo> docStructTemplateSlotVoList = docReportFragmentVo.getDocStructTemplateSlotVoList();
                        Map<Long, DocStructTemplateSlotVo> docStructTemplateSlotVoMap = docStructTemplateSlotVoList.stream().filter(vo -> ObjectUtils.isNotEmpty(vo)).collect(Collectors.toMap(DocStructTemplateSlotVo::getSlotSoid, e -> e));

                        DocStructTemplateSlotVo docStructTemplateSlotVo = docStructTemplateSlotVoMap.get(3537L);
                        if (ObjectUtils.isNotEmpty(docStructTemplateSlotVo)) {
                            DocStructSlotValueVo docStructSlotValueVo = docStructTemplateSlotVo.getDocStructSlotValueVo();
                            if (ObjectUtils.isNotEmpty(docStructSlotValueVo)) {
                                String dataValue = docStructSlotValueVo.getDataValue();
                                // 若修改换为上游姓名
                                if (!personName.equals(dataValue)) {
                                    docStructSlotValueVo.setDataValue(personName);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
